package com.dididoctor.doctor.Activity.Usercentre.Userinfo;

import android.content.Context;

import com.dididoctor.doctor.Activity.ConstantValue;
import com.dididoctor.doctor.Bean.Token;
import com.dididoctor.doctor.Http.MyHttpResponseHandler;
import com.dididoctor.doctor.MV.BasePresenter;
import com.dididoctor.doctor.MV.BusinessClient;
import com.dididoctor.doctor.MV.Response;
import com.dididoctor.doctor.Utils.Util;
import com.loopj.android.http.RequestParams;

import org.apache.http.Header;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2016/4/8.
 */
public class UserInfoPresenter extends BasePresenter {

    private UserInfoView view;

    public UserInfoPresenter(Context context, UserInfoView view) {
        super(context, view);
        this.view = view;
    }

    public void updateuserinfo(String name, String headPic, String sex, String cityCode, String hospital) {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("name", name);
        params.put("token", Token.getToken());
        params.put("headPic", headPic);
        params.put("sex", sex);
        params.put("cityCode", cityCode);
        params.put("hospital", hospital);
        BusinessClient.post(ConstantValue.userupdate, params, new MyHttpResponseHandler() {
            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    view.savesucced();
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.showToastMessage(throwable.getMessage());
                view.savefail();
            }
        });
    }

    public void getuserinfo() {
        RequestParams params = new RequestParams();
        params.put("userId", Token.getUserId());
        params.put("token", Token.getToken());
        BusinessClient.post(ConstantValue.userDetail, params, new MyHttpResponseHandler() {
            private List<Hospital> hospitals = new ArrayList<Hospital>();
            private List<Map<String, String>> maps = new ArrayList<Map<String, String>>();

            @Override
            public void onMyResponse(Response response) {
                if (response.isSucceed()) {
                    Userinfo userinfo = new Userinfo();
                    Map<String, Object> map = response.getMap("doctorInfo");
                    userinfo.setUserId(Util.toString(map.get("userId")));
                    userinfo.setPhone(Util.toString(map.get("phone")));
                    userinfo.setName(Util.toString(map.get("name")));
                    userinfo.setHeadPic(Util.toString(map.get("headPic")));
                    userinfo.setSex(Util.toString(map.get("sex")));
                    userinfo.setCity(Util.toString(map.get("city")));
                    userinfo.setCityName(Util.toString(map.get("cityName")));
                    maps = response.getListData("hosList");
                    for (Map<String, String> hosmap : maps) {
                        Hospital hospital = new Hospital();
                        hospital.setCityCode(Util.toString(hosmap.get("cityCode")));
                        hospital.setHosId(Util.toString(hosmap.get("hosId")));
                        hospital.setHospital(Util.toString(hosmap.get("hospital")));
                        hospital.setLatitude(Util.toString(hosmap.get("latitude")));
                        hospital.setLongitude(Util.toString(hosmap.get("longitude")));
                        hospitals.add(hospital);
                    }
                    view.getUserInfo(userinfo, hospitals);
                } else {
                    view.showToastMessage(response.getMsg());
                }
            }

            @Override
            public void onFailure(int i, Header[] headers, byte[] bytes, Throwable throwable) {
                super.onFailure(i, headers, bytes, throwable);
                view.getUserInfofail();
            }
        });
    }
}
